/**
 * Project Name:HomeActivity
 * Date:2016-1-21上午11:02:20
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.db.user.model;

import android.content.ContentValues;

import com.kkd.kkdapp.db.CurUserModel;
import com.kkd.kkdapp.db.annotate.EADBField;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldMode;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldType;
import com.kkd.kkdapp.db.app.model.User;
import com.kkd.kkdapp.db.base.EABaseModel;
import com.kkd.kkdapp.db.user.KkdDBHelper;

/**
 * ClassName: UserInfo <br/>
 * date: 2016-1-21 上午11:02:20 <br/>
 * 
 * @author askzhao
 * @version
 */
public class UserInfo extends EABaseModel {

	private static final String TABLE_NAME = UserInfo.class.getSimpleName();
	private static final int EXIST = -1000;

	// 用户ID
	@EADBField(mode = EADBFieldMode.NotNull, type = EADBFieldType.Text)
	public String customerId;

	// 手机号
	@EADBField(type = EADBFieldType.Text)
	public String mobile;

	// TODO 加密
	@EADBField
	public String pwd;

	// TODO 设备id，umeng推送使用
	@EADBField
	public String deviceId;

	// 客户端手机令牌
	@EADBField(type = EADBFieldType.Text)
	public String token;
	// 银行卡ID
	@EADBField
	public String cardId;
	// 登陆状态 0为已登陆
	@EADBField(type = EADBFieldType.Integer)
	public int state = -1;

	// 实名认证 1为已认证 0为未认证
	@EADBField(type = EADBFieldType.Integer)
	public int isRealNameAuth;

	// 用户名和身份证号
	@EADBField(type = EADBFieldType.Text)
	public String idCard;

	// 用户名
	@EADBField(type = EADBFieldType.Text)
	public String realName;
	// 银行卡绑定 1为已绑定 0为未绑定
	@EADBField(type = EADBFieldType.Integer)
	public int isBoundBankCard;

	// 签到 1为已签到 0为未签到
	@EADBField(type = EADBFieldType.Integer)
	public int signInFlag = 0;
	// 签到 是否绑定手机 1为已绑定 0为未绑定
	@EADBField(type = EADBFieldType.Integer)
	public int isBoundPhone;

	// 账户余额
	@EADBField(type = EADBFieldType.Integer)
	public double balance;

	// 是否显示过问卷调查
	@EADBField(type = EADBFieldType.Integer)
	public int isShowedAsses = -1; // -1未显示 0显示过
	// 是否做过问卷调查
	@EADBField(type = EADBFieldType.Integer)
	public int isSurved = -1; // -1 未评估 0评估过
	// 问卷调查链接
	@EADBField(type = EADBFieldType.Text)
	public String survedUrl;

	public void init(User user) {
		customerId = user.customerId;
		mobile = user.mobile;
		pwd = user.pwd;
		deviceId = user.deviceId;
		token = user.token;
		cardId = user.cardId;
		state = user.state;
		isRealNameAuth = user.isRealNameAuth;
		idCard = user.idCard;
		realName = user.realName;
		isBoundBankCard = user.isBoundBankCard;
		signInFlag = user.signInFlag;
		isBoundPhone = user.isBoundPhone;
	}

	public boolean updateUserPwd(String pwd) {
		ContentValues values = new ContentValues();
		values.put("pwd", pwd);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values,
				"customerId=?", new String[] { customerId }) > 0;
	}

	public boolean updateCardId(String cardid) {
		ContentValues values = new ContentValues();
		values.put("cardid", cardid);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values,
				"customerId=?", new String[] { customerId }) > 0;
	}

	public boolean updateToken(String token) {
		ContentValues values = new ContentValues();
		values.put("token", token);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values,
				"customerId=? ", new String[] { customerId }) > 0;
	}

	public boolean updateBalance(double balance) {
		ContentValues values = new ContentValues();
		values.put("balance", balance);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values,
				"customerId=? ", new String[] { customerId }) > 0;
	}

	public boolean updateIsShowedAsses(int isShowedAsses) {
		System.out.println("UserInfo.updateIsSurved()");
		ContentValues values = new ContentValues();
		values.put("isShowedAsses", isShowedAsses);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values,
				"customerId=?", new String[] { customerId }) > 0;
	}

	public static boolean updateAsses(int isShowedAsses, String url) {
		System.out.println("UserInfo.updateIsSurved()");
		ContentValues values = new ContentValues();
		values.put("isShowedAsses", isShowedAsses);
		values.put("isSurved", isShowedAsses);
		values.put("survedUrl", url);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values,
				"customerId=?",
				new String[] { CurUserModel.getCurUser().customerId }) > 0;
	}

	/**
	 * queryUserInfoByCid:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 * @author askzhao
	 * @param customerId
	 * @return
	 */
	public static UserInfo queryUserInfoByCid(String customerId) {
		return KkdDBHelper.getInstance().queryTopOne(UserInfo.class,
				"customerId=? ", new String[] { customerId });
	}

	public static long insertOrUpdate(UserInfo userInfo) {
		UserInfo curUser = queryUserInfoByCid(userInfo.customerId);
		if (curUser != null) {
			return EXIST;
		} else {
			return KkdDBHelper.getInstance().saveBindId(userInfo);
		}
	}

}